package com.tfswx.ruleengine.compute.gzjy.felfunc;

import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.context.ContextChain;
import com.greenpineyu.fel.context.FelContext;
import com.greenpineyu.fel.context.MapContext;
import com.tfswx.ruleengine.compute.bean.DataObject;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/6/11
 */
public class GetZdProp extends BaseFn {
    @Override
    public Object call(Object[] arguments, FelContext context) {
        DataObject dataObject = (DataObject) arguments[0];
        String arg = String.valueOf(arguments[1]);
        String exp = String.valueOf(arguments[2]);
        List<String> logs = service.getGzjyLog().getLogs();
        if (StringUtils.isBlank(exp)) {
            logs.add("GetZdProp：第三个参数为空");
            return "";
        }
        DataObject.ZdItemDTO zd = getZd(dataObject, arg, true);
        if (zd == null) {
            logs.add("GetZdProp：没有找到字段");
            return "";
        }

        FelEngine fel = (FelEngine) context.get("fel");
        FelContext ctx = new ContextChain(context, new MapContext());
        ctx.set("data", zd);
        Object eval = fel.eval("data." + exp, ctx);
        logs.add("GetZdProp：获取到值==>" + eval);
        return eval;
    }

    @Override
    public String getZwmc() {
        return "获取字段属性值";
    }

    @Override
    public boolean isExport() {
        return true;
    }

    @Override
    public boolean isBuiltIn() {
        return true;
    }

    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("$data"));
        params.add(new Param("zdmc"));
        params.add(new Param("propMc"));
        return params;
    }

    @Override
    public String getName() {
        return "getZdProp";
    }
}
